package com.dz.scrm.mvp.ui

import android.app.Activity
import android.content.Intent
import android.net.Uri
import android.os.Build
import android.os.Bundle
import android.provider.Settings
import androidx.core.content.FileProvider
import com.dz.scrm.R
import com.dz.scrm.constant.INTENT_APK_PATHL
import com.dz.scrm.constant.INTENT_REQUEST_INSTALL
import com.dz.scrm.utils.*
import com.miui.enterprise.sdk.PhoneManager
import com.zackratos.ultimatebarx.ultimatebarx.UltimateBarX
import com.zackratos.ultimatebarx.ultimatebarx.bean.BarConfig
import me.jessyan.art.base.BaseActivity
import me.jessyan.art.mvp.IPresenter
import me.jessyan.art.mvp.IView
import java.io.File
import java.util.HashMap

/**
 * 作者：fanda
 * 日期：2021/4/23 11:32
 * 描述：
 */
abstract class DZActivity<P : IPresenter> : BaseActivity<P>(), IView {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val config = BarConfig.newInstance()            // 创建配置对象
                .fitWindow(false)                        // 布局是否侵入状态栏（true 不侵入，false 侵入）
                .colorRes(R.color.white)
                .light(true)                               // light模式
        // 状态栏字体 true: 灰色，false: 白色 Android 6.0+
        // 导航栏按钮 true: 灰色，false: 白色 Android 8.0+
        UltimateBarX.with(this)                         // 对当前 Activity 或 Fragment 生效
                .config(config)                             // 使用配置
                .applyStatusBar()                           // 应用到状态栏

    }

    override fun showLoading() = DialogUtil.showProgressDialog(this)

    override fun hideLoading() = DialogUtil.dismissDialog()

    fun checkInstallPermission(apkFilePath: String) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            if (packageManager.canRequestPackageInstalls()) {
                installApk(apkFilePath)
            } else {
                val packageURI = Uri.parse("package:$packageName")
                val intent = Intent(Settings.ACTION_MANAGE_UNKNOWN_APP_SOURCES, packageURI)
                intent.putExtra(INTENT_APK_PATHL, apkFilePath)
                startActivityForResult(intent, INTENT_REQUEST_INSTALL)
            }
        } else {
            installApk(apkFilePath)
        }
    }

    /**
     * 安装APK
     */
    private fun installApk( apkFilePath: String) {
        if (!StringUtils.isEmpty(apkFilePath)) {
            val file = File(apkFilePath)
            val intent = Intent(Intent.ACTION_VIEW)
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                val uri = FileProvider.getUriForFile(this, applicationContext.packageName.toString() + ".fileprovider", file)
                intent.setDataAndType(uri, "application/vnd.android.package-archive")
                intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
            } else {
                intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive")
            }
            startActivity(intent)

        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == INTENT_REQUEST_INSTALL) {
            if (resultCode == Activity.RESULT_OK) {
                installApk( data?.getStringExtra(INTENT_APK_PATHL) ?: "")
            } else {
                val checkBean = getCheckVersionBean()
                checkBean?.let {
                    if (checkBean.upgrade!!) {
                        checkInstallPermission(data?.getStringExtra(INTENT_APK_PATHL) ?: "")
                    }
                }
            }
        }
    }

    fun isHidenPhoneNum(isHide : Boolean) {
        val map: MutableMap<Int, String> = HashMap()
        if (isHide) {
            //表示加密
            map[11] = "___****____"
            PhoneManager.getInstance().setPhoneNumberHide(map,AppUtils.getPackageName(this))
        } else {
            map[11] = "___________"
            PhoneManager.getInstance().setPhoneNumberHide(map,AppUtils.getPackageName(this))
        }
    }

}